#ifndef PLAYER_H
#define PLAYER_H

#include <allegro5/allegro5.h>
#include "midi.h"
class Song;
class Event;
class Track;
class Pattern;

class Player {
public:
     enum Mode {
	  PlayPattern,
	  PlaySong
     };

     Player();
     ~Player();
     void setSong(Song *pSong);
     void setMode(Mode mode);
     void setRepeat(bool repeat);
     int songItemNum();
     void setSongItemNum(int num);
     void startPlaying();
     void stopPlaying();
     void togglePlaying();
     void quit();
     void updateTempo();
     void start();
     void run();
     int row();
     void setRow(int row);
     bool playing();
     // play a note immediately
     void playNote(int num, int inst);
     void patternChanged(Pattern *pPat, int eventIndex);

private:     
     static const int NoteLength = 100;
     static const int Velocity = 0x40;
     Midi m_midi;
     Song *m_pSong;
     bool m_repeat;
     int m_songItemNum;
     Mode m_mode;
     bool m_playing;
     bool m_quit;
     ALLEGRO_THREAD *m_pThread;
     int m_row;
     int m_startRow;
     int m_requeue;

     void scheduleLoopEvent(int row, int rowOffset=0);
     void scheduleEvent(Event *pEvent, int row, Track *pTrack, int rowOffset=0);
     void doEvents();
     void scheduleEvents(int rowOffset=0);
     int rowTick(int row);
     void connectPatternChange();
     int msToTicks(int ms);
};

#endif
